package com.feihong.txgw.adapter.delivery;

import android.text.TextUtils;

import com.adapter.base.BaseQuickAdapter;
import com.adapter.base.BaseViewHolder;
import com.adapter.base.animation.ScaleInAnimation;
import com.amap.api.maps.AMapUtils;
import com.amap.api.maps.model.LatLng;
import com.amap.api.services.core.LatLonPoint;
import com.amap.api.services.core.PoiItem;
import com.feihong.txgw.R;
import com.feihong.txgw.utils.UserLoginInfo;

import java.math.BigDecimal;

/**
 * @Author wangziheng
 * @ProjectName txgw_app
 * @ClassName DeliverySearchPoiDataReAdapter
 * @package com.feihong.txgw.adapter.delivery
 * @data 2018/6/7 11:48
 * @Describe
 * @Change
 */
public class DeliverySearchPoiDataReAdapter extends BaseQuickAdapter<PoiItem,BaseViewHolder> {
    /**
     * 用户当前位置
     */
    private LatLng mUserLatLng;
    public DeliverySearchPoiDataReAdapter(int layoutResId) {
        super(layoutResId);
        openLoadAnimation(new ScaleInAnimation(0.8f));
        isFirstOnly(false);
        loadMoreEnd(true);
        double lat = TextUtils.isEmpty(UserLoginInfo.getUserCityInfo().getLatitude()) ? 0 : Double.valueOf(UserLoginInfo.getUserCityInfo().getLatitude());
        double lnt = TextUtils.isEmpty(UserLoginInfo.getUserCityInfo().getLongitude()) ? 0 : Double.valueOf(UserLoginInfo.getUserCityInfo().getLongitude());
        mUserLatLng = new LatLng(lat, lnt);
    }

    @Override
    protected void convert(BaseViewHolder helper, PoiItem item) {
        String desStr = item.getAdName().trim()+item.getSnippet();
        LatLonPoint latLonPoint = item.getLatLonPoint();
        LatLng mDistinationLatLng = new LatLng(latLonPoint.getLatitude(),latLonPoint.getLongitude());
        float d = AMapUtils.calculateLineDistance(mDistinationLatLng,mUserLatLng);
        String Distance;
        //设置位数
        int   scale  =   1;
        //表示四舍五入，可以选择其他舍值方式，例如去尾，等等.
        int   roundingMode  =  4;
        if(d > 1000){
            d =  (d / 1000);
            BigDecimal bd  =   new BigDecimal((double)d);
            bd   =  bd.setScale(scale,roundingMode);
            d   =  bd.floatValue();
            Distance = String.valueOf(d+"km");
        }else {
            BigDecimal   bd  =   new BigDecimal((double)d);
            bd   =  bd.setScale(scale,roundingMode);
            d   =  bd.floatValue();
            Distance = String.valueOf(d+"m");
        }
        helper.setText(R.id.delivery_adr_search_item_name,item.toString());
        helper.setText(R.id.delivery_adr_search_item_des,desStr);
        helper.setText(R.id.delivery_adr_search_item_distance,Distance);
    }
}
